//
//  UIViewController+PresentViewContoller.m
//  etionUI
//
//  Created by wjxfhxy on 15/9/13.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "UIViewController+PresentViewContoller.h"

#import "XUIPanelWindow.h"

@implementation UIViewController (PresentViewContoller)

static NSMutableArray *s_cacheRoot;

+(void)setCacheRootViewController:(UIViewController*)root
{
    if(s_cacheRoot == nil)
        s_cacheRoot = [NSMutableArray new];
    [s_cacheRoot addObject:root];
}

+(UIViewController*)cacheRootViewController
{
    UIViewController *root = s_cacheRoot.lastObject;
    [s_cacheRoot removeLastObject];
    return root;
}

+ (void)presentRootViewControllerClassOfNavigationViewController:(Class)rootViewControllerClass animated: (BOOL)flag completion:(void (^)(void))completion
{
    [UIViewController presentRootViewControllerClassOfNavigationViewController:rootViewControllerClass switchParam:nil animated:flag completion:completion];
}

+ (void)presentRootViewControllerClassOfNavigationViewController:(Class)rootViewControllerClass switchParam:(XUIViewControllerSwitchParam *)switchParam animated:(BOOL)flag completion:(void (^)(void))completion
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if([window isKindOfClass:[XUIPanelWindow class]])
    {
        UIViewController *nav = [[UIApplication sharedApplication].delegate window].rootViewController;
        
        [UIViewController setCacheRootViewController:nav];
        
        UIViewController *viewController = [[[rootViewControllerClass alloc] initWithViewControllerSwitchParam:switchParam] autorelease_mm];
        
        XUINavigationController *nar = [[[XUINavigationController alloc]initWithRootViewController:viewController] autorelease_mm];
        [nav presentViewController:nar animated:YES completion:completion];
    }
    else
    {
        UIViewController *nav = window.rootViewController;
        
        [UIViewController setCacheRootViewController:nav];
        
        UIViewController *viewController = [[[rootViewControllerClass alloc] initWithViewControllerSwitchParam:switchParam] autorelease_mm];
        
        XUINavigationController *nar = [[[XUINavigationController alloc]initWithRootViewController:viewController] autorelease_mm];
        [nav presentViewController:nar animated:YES completion:completion];
    }
}

+ (void)presentViewControllerClass:(Class)viewControllerClass animated:(BOOL)flag completion:(void (^)(void))completion
{
    [UIViewController presentViewControllerClass:viewControllerClass switchParam:nil animated:flag completion:completion];
}

+ (void)presentViewControllerClass:(Class)viewControllerClass switchParam:(XUIViewControllerSwitchParam *)switchParam animated:(BOOL)flag completion:(void (^)(void))completion
{
    UIViewController *vcl = [[[viewControllerClass alloc] initWithViewControllerSwitchParam:switchParam] autorelease_mm];
    
    [UIViewController presentViewController:vcl animated:flag completion:completion];
}

+ (void)presentViewController:(UIViewController*)viewController animated:(BOOL)flag completion:(void (^)(void))completion
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if([window isKindOfClass:[XUIPanelWindow class]])
    {
        UIViewController *rootViewController = [[UIApplication sharedApplication].delegate window].rootViewController;
        
        [UIViewController setCacheRootViewController:rootViewController];
        
        [rootViewController presentViewController:viewController animated:YES completion:completion];
    }
    else
    {
        UIViewController *rootViewController = window.rootViewController;
        
        [UIViewController setCacheRootViewController:rootViewController];
        
        [rootViewController presentViewController:viewController animated:YES completion:completion];
    }
}

+ (void)dismissViewControllerAnimated:(BOOL)flag switchParam:(XUIViewControllerSwitchParam*)switchParam completion:(void (^)(void))completion
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIView *lastView = [keyWindow.subviews lastObject];
    if([UIDevice isIOS8OrNewer])
    {
        if([lastView isMemberOfClass:NSClassFromString(@"UITransitionView")])
        {
            UIView *view = [lastView.subviews lastObject];
            UIViewController *vcl = [UIViewController superViewController:view];
            if([vcl isKindOfClass:[UIViewController class]])
            {
                if([vcl isMemberOfClass:[UIViewController class]])
                {
                   [vcl viewControllerWillDealloc];
                }
                else if([vcl isMemberOfClass:[XUINavigationController class]])
                {
                    NSArray *ar = ((XUINavigationController*)vcl).viewControllers;
                    [ar makeObjectsPerformSelector:@selector(viewControllerWillDealloc)];
                }
                
                UIViewController *preRoot = [UIViewController cacheRootViewController];
                if([preRoot isMemberOfClass:[XUINavigationController class]])
                    [preRoot.topViewController recvDismissViewControllerSwitchParam:switchParam];
                
                [vcl dismissViewControllerAnimated:flag completion:completion];
            }
        }
    }
    else
    {
        UIWindow *window = nil;
        if([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)])
            window = [[UIApplication sharedApplication].delegate window];
        
        if(window != nil)
        {
            if(lastView.superview == window)
            {
                UIViewController *vcl = [UIViewController superViewController:lastView];
                if(vcl != nil)
                {
                    UIViewController *preRoot = [UIViewController cacheRootViewController];
                    if([preRoot isMemberOfClass:[XUINavigationController class]])
                        [preRoot.topViewController recvDismissViewControllerSwitchParam:switchParam];
                    
                    [vcl dismissViewControllerAnimated:flag completion:completion];
                }
            }
        }
    }
}

+ (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [UIViewController dismissViewControllerAnimated:flag switchParam:nil completion:completion];
}

+ (void)dismissViewControllerAnimated:(BOOL)flag postNotificationKey:(NSString*)key userInfo:(NSDictionary*)userInfo
{
    [UIViewController dismissViewControllerAnimated:flag completion:^
    {
        if(key.length > 0)
        {
            [NSNotificationCenter postNotification:key userInfo:userInfo];
        }
    }];
    
}

-(void)recvDismissViewControllerSwitchParam:(XUIViewControllerSwitchParam *)switchParam
{
    
}

@end
